package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.nio.file.Watchable;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description:套餐管理
 * @ClassName: SetmealController
 * @Author: xmc
 * @Date: 2022/8/18 6:28
 * @Version: 1.0
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto){

        log.info("接收的套餐信息:{}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("保存成功");

    }

   /**
    *
    *方法说明:分页信息
    * @author xmc
    * @param page
    * @param pageSize
    * @return com.itheima.reggie.common.R<java.util.List<com.itheima.reggie.dto.SetmealDto>>
    * @date 2022/8/18 7:45
    */

    @GetMapping("/page")
        public R<Page> page(int page, int pageSize,String name){


        //构造分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);
        Page<SetmealDto> setmealDtoPage = new Page<>(page,pageSize);
        //构造分页条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        //根据name进行模糊查询
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //添加排序条件
        wrapper.orderByAsc(Setmeal::getUpdateTime);
        setmealService.page(pageInfo,wrapper);
        BeanUtils.copyProperties(pageInfo,setmealDtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();
        List<SetmealDto> setmealDtos = records.stream().map((item) ->{
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item,setmealDto);
            Category category = categoryService.getById(item.getCategoryId());
            if (category!= null){
                setmealDto.setCategoryName(category.getName());
            }
            return setmealDto;
        }).collect(Collectors.toList());
        setmealDtoPage.setRecords(setmealDtos);

        return R.success(setmealDtoPage);

    }

    @DeleteMapping
    @Transactional
    public R<String> del(@RequestParam List<Long> ids){
        setmealService.deleteWithDish(ids);
        return R.success("删除成功");
    }

    @PostMapping("/status/{stus}")
    public R<String> status(@PathVariable int stus,@RequestParam List<Long> ids){
        log.info("接收到stus:{}",stus);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        for (Setmeal setmeal : setmealList) {
            setmeal.setStatus(stus);
            setmealService.updateById(setmeal);
        }
        return R.success("停售成功");
    }
    /**
     *
     *方法说明:通过套餐id查询套餐及其菜品
     * @author xmc
     * @param id
     * @return com.itheima.reggie.common.R<com.itheima.reggie.dto.SetmealDto>
     * @date 2022/8/18 18:23
     */

    @GetMapping("{id}")
    public R<SetmealDto> getById(@PathVariable Long id){
        SetmealDto setmealDto = setmealService.getWithDishById(id);
        return R.success(setmealDto);

    }

    /**
     *
     *方法说明:更新套餐信息
     * @author xmc
     * @param setmealDto
     * @return com.itheima.reggie.common.R<java.lang.String>
     * @date 2022/8/18 18:27
     */

    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto){
        setmealService.updateWithDish(setmealDto);
        return R.success("更新成功!");
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(setmeal.getCategoryId() != null,Setmeal::getCategoryId,setmeal.getCategoryId());
        wrapper.eq(Setmeal::getStatus,setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> setmealList = setmealService.list(wrapper);
        log.info(setmealList.toString());
        return R.success(setmealList);

    }

}
